﻿using System;
using System.Collections.Generic;
using System.Linq;
using Backend.JYD.Business;
using Backend.JYD.Model;
using Backend.JYD.Model.Task;

namespace Backend.JYD.BLL.Task
{
    public class TaskStepModelBLL
    {
        private static readonly IDAL.Task.ITaskStepModel idal = Factory.DALFactory.CreateEFDAL<IDAL.Task.ITaskStepModel>("DAL.TaskStepModelDAL");
        
        public Int64 AddTaskStepModel(TaskStepModel info)
        {
            if (string.IsNullOrWhiteSpace(info.StepName)) return 0;
            if (info.TaskModeId <= 0) return 0;
            if (info.StepId <= 0) return 0;
            //不能结束，但又没有下一步，返回错误。
            if (!info.AllowStop && string.IsNullOrWhiteSpace(info.NextStepIds)) return 0;
            //第一步，步能退回
            if (info.StepId == 1 && !string.IsNullOrWhiteSpace(info.LastStepIds)) return 0;
            //使用角色，但能指定的角色为空。 如果是全部？是否有数量限制
            if (info.UseRole && string.IsNullOrWhiteSpace(info.RoleIds)) return 0;
            //直接指定用户，但能指定的用户为空。如果是全部？是否有数量限制
            if (!info.UseRole && string.IsNullOrWhiteSpace(info.EmpIds)) return 0;
            //指定权限？

            var id = idal.AddTaskStepModel(info);
            TaskManager.ReloadAllTaskStepModeles(info.TaskModeId);
            return id;
            //return idal.AddTaskStepModel(info);
        }

        public bool UpdateTaskStepModel(TaskStepModel info)
        {
            if (string.IsNullOrWhiteSpace(info.StepName)) return false;
            if (info.TaskModeId <= 0) return false;
            if (info.TaskStepId <= 0) return false;
            if (info.StepId <= 0) return false;
            //不能结束，但又没有下一步，返回错误。
            if (!info.AllowStop && string.IsNullOrWhiteSpace(info.NextStepIds)) return false;
            //第一步，步能退回
            if (info.StepId ==1 && !string.IsNullOrWhiteSpace(info.LastStepIds)) return false;
            //使用角色，但能指定的角色为空。 如果是全部？是否有数量限制
            if (info.UseRole && string.IsNullOrWhiteSpace(info.RoleIds)) return false;
            //直接指定用户，但能指定的用户为空。如果是全部？是否有数量限制
            if (!info.UseRole && string.IsNullOrWhiteSpace(info.EmpIds)) return false;
            //指定权限？

            var success = idal.UpdateTaskStepModel(info);
            TaskManager.ReloadAllTaskStepModeles(info.TaskModeId);
            return success;
            //return idal.UpdateTaskStepModel(info);
        }

        public TaskStepModel GetTaskStepModelByTaskStepId(Int64 taskStepId)
        {
            if (taskStepId <= 0) return null;

            var list = TaskManager.LoadAllTaskStepModel();
            var temp = list == null ? null : list.Where(p => taskStepId == p.TaskStepId);
            return temp == null ? null : temp.FirstOrDefault();
            //return idal.GetTaskStepModelByTaskStepId(taskStepId);
        }

        public IList<TaskStepModel> GetTaskStepModelByTaskStepIds(IList<Int64> taskStepIds)
        {
            if (taskStepIds != null || taskStepIds.Count <= 0) return null;
            var list = TaskManager.LoadAllTaskStepModel();
            var temp = list == null ? null : list.Where(p => taskStepIds.Contains(p.TaskStepId));
            return temp == null ? null : temp.ToList();
            //return idal.GetTaskStepModelByTaskStepIds(taskStepIds);
        }

        public List<TaskStepModel> GetTaskStepModelByTaskModeId(Int64 taskModeId)
        {
            if (taskModeId <= 0) return null;
            var list = TaskManager.LoadAllTaskStepModel();
            var temp = list == null ? null : list.Where(p => taskModeId == p.TaskModeId);
            return temp == null ? null : temp.ToList();
            //var list = idal.GetTaskStepModelByTaskModeId(taskModeId);
            //return list == null ? null : list.ToList();
        }

        public TaskStepModel GetTaskStepModelByTaskModeIdAndStepId(Int64 taskModeId, int stepId)
        {
            if (taskModeId <= 0 || stepId <= 0) return null;
            var list = TaskManager.LoadAllTaskStepModel();
            var temp = list == null ? null : list.Where(p => taskModeId == p.TaskModeId && stepId == p.StepId);
            return temp == null ? null : temp.FirstOrDefault();
            //return idal.GetTaskStepModelByTaskModeIdAndStepId(taskModeId, stepId);
        }

        public PageInfo<TaskStepModel> GetTaskStepModelByPage(PageInfo page)
        {
            var list = TaskManager.LoadAllTaskStepModel();
            return new PageInfo<TaskStepModel>(page, list);
            //return idal.GetTaskStepModelByPage(page);
        }

    }
}
